/**
 * Copyright (C), 2015-2018, 成都启迪信息技术有限公司
 * FileName: WlController
 * Author:   sjkyll
 * Date:     2018-08-09 11:06
 * Description: 微网通联状态报告接口，用于接收对方的状态报告推送服务
 * <author>          <time>          <version>          <desc>
 * 作者姓名         修改时间           版本号            描述
 */
package com.cdqidi.smsservice.controller.api;

/**
 * 〈一句话功能简述〉<br>
 *
 * @author sjkyll
 * @create 2018-08-09 11:06
 * @since 1.0.0
 */

import com.cdqidi.smsservice.dto.Sms;
import com.cdqidi.smsservice.dto.SmsReport;
import com.cdqidi.smsservice.exception.ApiException;
import com.cdqidi.smsservice.service.SmsReportService;
import com.cdqidi.smsservice.service.sms.SmsService;
import com.cdqidi.smsservice.util.IpHelper;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.Pageable;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.servlet.http.HttpServletRequest;
import java.util.ArrayList;
import java.util.List;

@Controller
@Slf4j
public class SmsReportController {
    private final SmsService smsService;
    private final SmsReportService smsReportService;

    @Autowired
    public SmsReportController(SmsService smsService,SmsReportService smsReportService) {
        this.smsService = smsService;
        this.smsReportService = smsReportService;
    }

    @RequestMapping(value = "/sms/report/receiveMsg", method = RequestMethod.POST)
    @ResponseBody
    public boolean smsReport(SmsReport report, HttpServletRequest request) {
        String ip = IpHelper.getIpAddr(request);
        log.debug("请求ip: " + ip + ",AccountID: " + report.getAccountID() + ",MsgID: " + report.getMsgID() + ",MobilePhone: " + report.getMobilePhone() + ",ReportResultInfo: " + report.getReportResultInfo() +",ReportState: "+report.getReportState()+",ClientMsgId: " + report.getClientMsgId());
        boolean isLog = false;
        if(ip.contains("124.251.77")){
            isLog=true;
        }
        if(!isLog){
            if(ip.equals("127.0.0.1")){
                isLog = true;
            }
        }
        if(isLog){
            smsService.saveReportLogs(report);
            return true;
        }
        return false;
    }

    @RequestMapping(value = "/api/zwReport", method = RequestMethod.GET)
    @ResponseBody
    public List<String> zwReport(String reqId) throws ApiException {
        String report = smsReportService.getZwReport(reqId);
        ArrayList<String> list = new ArrayList<>();
        list.add(report);
        return list;
    }

    @RequestMapping(value = "/api/smsReport",method=RequestMethod.GET)
    @ResponseBody
    public Page<Sms> getSmsReport(String mobile, Pageable pageable){
        return smsReportService.getSmsReport(mobile,pageable);
    }
}